package com.zmn.plat.admin.controller.channel.servicearea;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.plat.model.entity.channel.ChannelBatchAreaServCategoryVO;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.plat.admin.controller.channel.ChannelBaseController;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.channel.ChannelServiceArea;
import com.zmn.plat.services.interfaces.channel.ChannelServiceAreaService;

@Controller
@RequestMapping("/channel/service/area/")
public class ChannelServiceAreaModifyController extends ChannelBaseController {
    @Resource
    ChannelServiceAreaService channelServiceAreaService;

    private Logger logger = LoggerFactory.getLogger(ChannelServiceAreaModifyController.class);

    /**
     * region 新增/修改
     *
     * @param request
     * @param channelServiceArea
     * @return
     */
    @RequestMapping(value = "drawChannelAreaMap")
    @RequiresPermissions(value = {"/channel/serviceArea/drawMap/addEdit.action", "/channel/add.action"}, logical = Logical.OR)
    @ResponseBody
    public ResponseDTO drawChannelAreaMap(HttpServletRequest request, @RequestBody ChannelServiceArea channelServiceArea) {
        try {
            channelServiceArea.setUpdater(getMcStaff().getRealName());
            channelServiceArea.setUpdateTime(DateUtil.getNow());
            channelBService.modifyDrawChannelAreaMap(channelServiceArea);
            return ResponseDTO.success(Integer.toString(channelServiceArea.getServiceAreaId()), "操作成功！");
        } catch (Exception ex) {
            logger
                .error(String.format("[zmn]新增/修改公司失败,category:[%s],ex:[%s]", JSONObject.toJSONString(channelServiceArea), ex.getMessage()));
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 保存区域服务分类
     *
     * @param serviceAreaId
     *            the service area id
     * @param servCategId
     *            服务分类ID
     * @param channelId
     *            the channel id
     * @param showType
     *            产品分类 1后台 2前台
     * @param categoryId
     *            the category id
     * @return the result dto
     */
    @RequestMapping("saveServiceCategory")
    @RequiresPermissions(value = {"/channel/serviceArea/serviceCategory/addEdit.action", "/channel/add.action"}, logical = Logical.OR)
    @ResponseBody
    public ResponseDTO saveServiceCategory(Integer serviceAreaId, Integer servCategId, Integer channelId, Integer showType,
        String categoryId) {
        String lable = "保存区域服务分类";
        try {
            LoginStaff mcStaff = getMcStaff();
            if (channelId == null) {
                return ResponseDTO.fail("渠道ID不能为空！");
            }
            if (servCategId == null) {
                return ResponseDTO.fail("服务分类ID不能为空！");
            }

            List<String> ids = StringUtil.isBlank(categoryId) ? Collections.EMPTY_LIST : Arrays.asList(categoryId.split(","));
            channelBService.modifyChannelServiceAreaCategory(serviceAreaId, servCategId, channelId, showType, ids, mcStaff.getRealName());

            return ResponseDTO.success(serviceAreaId,String.format("%s成功！", lable));

        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("error:", e);
            return  ResponseDTO.fail(String.format("%s失败！", lable));
        }
    }

    @RequestMapping("batchSaveServiceCategory")
    @RequiresPermissions(value = {"/channel/serviceArea/serviceCategory/addEdit.action", "/channel/add.action"}, logical = Logical.OR)
    @ResponseBody
    public ResponseDTO batchSaveServiceCategory(@RequestBody ChannelBatchAreaServCategoryVO batchAreaServCategoryVO) {
        String lable = "批量保存区域服务分类";
        try {
            LoginStaff mcStaff = getMcStaff();
            if (Objects.isNull(batchAreaServCategoryVO.getChannelId())) {
                return ResponseDTO.fail("渠道ID不能为空！");
            }
            if (Objects.isNull(batchAreaServCategoryVO.getServCategId())) {
                return ResponseDTO.fail("服务分类ID不能为空！");
            }

            batchAreaServCategoryVO.setOperator(mcStaff.getRealName());
            channelBService.batchModifyChannelServiceAreaCategory(batchAreaServCategoryVO);

            return ResponseDTO.success(GlobalConsts.YES, String.format("%s成功！", lable));
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("error:", e);
            return  ResponseDTO.fail(String.format("%s失败！", lable));
        }
    }

    /**
     * 移除区域
     *
     * @param ids
     *            the ids
     * @return the result dto
     */
    @RequestMapping(value = "remove")
    @RequiresPermissions(value = {"/channel/serviceArea/del.action", "/channel/add.action"}, logical = Logical.OR)
    @ResponseBody
    public ResponseDTO remove(String ids) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择区域！");
        }
        LoginStaff mcStaff = getMcStaff();
        List<Integer> serviceAreaIds = StringUtil.convertStrsToList(ids);
        channelBService.deleteServiceArea(serviceAreaIds, mcStaff.getRealName());
        return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功！");
    }

    /**
     * 新增区域
     *
     * @param ids
     *            the ids
     * @param channelId
     *            渠道ID
     * @return the result dto
     */
    @RequestMapping(value = "add")
    @RequiresPermissions(value = {"/channel/serviceArea/addEdit.action", "/channel/add.action"}, logical = Logical.OR)
    @ResponseBody
    public ResponseDTO add(String ids, Integer channelId) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择区域！");
        }
        if (channelId == null || channelId == 0) {
            return ResponseDTO.fail("渠道ID不能为空！");
        }
        LoginStaff mcStaff = getMcStaff();
        List<Integer> areaIds = StringUtil.convertStrsToList(ids);
        try {
            channelBService.addServiceArea(areaIds, channelId, mcStaff.getRealName());
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("系统错误! 操作失败!");
        }

        return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功！");
    }

    /**
     * 新增全国城市服务区域
     *
     * @param channelId
     *            渠道ID
     * @return the result dto
     */
    @RequestMapping(value = "addAllCity")
    @RequiresPermissions(value = {"/channel/serviceArea/addEdit.action", "/channel/add.action"}, logical = Logical.OR)
    @ResponseBody
    public ResponseDTO addAllCity(Integer channelId) {
        if (channelId == null || channelId == 0) {
            return ResponseDTO.fail("渠道ID不能为空！");
        }
        LoginStaff mcStaff = getMcStaff();
        try {
            channelBService.addAllCityServiceArea(channelId, mcStaff.getRealName());
        } catch (PlatException e) {
            return  ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("error:", e);
            return  ResponseDTO.fail("系统错误! 操作失败!");
        }

        return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功！");
    }
}
